{extend name="public/container"}
{block name="content"}
<div id="myApp">
    <div class="douxue-filter padding-10 mb-20 flex flex-between" style="margin-bottom:20px">
        <el-form :inline="true" :model="where" size="mini" class="flex">
            <div class="flex flex-between">
                <el-form-item>
                    <el-select size="small" v-model="where.roles" placeholder="请选择身份" :clearable=true>
                        <el-option v-for="(item,index) in roles" :label="item" :value="index"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-input size="small" v-model="where.name" placeholder="请输入姓名或者账号">
                        <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                    </el-input>
                </el-form-item>
            </div>
        </el-form>

        <el-button type="primary" size="mini" @click="doAdd()">新增管理员</el-button>
    </div>

    <div class="padding-10">
        <el-table
            :data="rows"
            style="width: 100%"
            v-loading="loading"
            element-loading-text="加载中"
            element-loading-spinner="el-icon-loading"
            size="mini">
            <el-table-column
                    prop="real_name"
                    label="姓名"
                    width="200"
                    align="left">
            </el-table-column>
            <el-table-column
                    prop="account"
                    label="账号"
                    width="160"
                    align="left">
            </el-table-column>
            <el-table-column
                    prop="roles"
                    label="身份"
                    align="left">
                <template slot-scope="scope">
                    <b v-if="scope.row.id==1">超级管理员</b>
                    <el-tag
                        v-else
                        v-for="role in scope.row.rolesData"
                        :key="role.id"
                        size="mini">
                        {{role.role_name}}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="last_time"
                    label="最后登陆时间"
                    width="150"
                    cell-click="cell"
                    align="left">
            </el-table-column>
            <el-table-column
                    prop="last_ip"
                    label="最后登陆ip"
                    width="120"
                    cell-click="cell"
                    align="left">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="是否显示"
                    width="120"
                    align="left">
                <template slot-scope="scope">
                    <el-switch
                            v-model="scope.row.status"
                            active-color="#ff6a6c"
                            inactive-color="#999"
                            :active-value="1"
                            :inactive-value="0"
                            @change="doSetStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                    label=""
                    width="90"
                    align="left">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary" @click="doAdd(scope.row)" icon="el-icon-edit"></el-button>
                    <el-button size="mini" type="danger" @click="doDelete(scope.row.id)" icon="el-icon-delete"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                    small
                    :hide-on-single-page="true"
                    layout="prev, pager, next"
                    :total="rowsCount"
                    :page-size="where.limit"
                    @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>
    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" :visible.sync="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="100px" >
            <el-form-item label="管理员账号" prop="account">
                <el-input v-model="formData.account" placeholder="管理员账号" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="管理员密码" prop="pwd">
                <el-input v-model="formData.pwd" type="password" placeholder="管理员密码" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="conf_pwd">
                <el-input v-model="formData.conf_pwd" type="password" placeholder="确认密码" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="管理员姓名" prop="real_name">
                <el-input v-model="formData.real_name" placeholder="管理员姓名" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="管理员身份" prop="roles">
                <el-select v-model="formData.roles" :clearable=true multiple placeholder="请选择" style="width: 100%">
                    <el-option v-for="(item,index) in roles" :key="index" :label="item" :value="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="formData.status">
                    <el-radio :label="1">显示</el-radio>
                    <el-radio :label="0">不显示</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var roles = <?=isset($role) ? json_encode($role) : "[]"?>;
    var my = new Vue({
        el: "#myApp",
        data() {
            var validateConfPass = (rule, value, callback) => {
                if(this.formData.id != 0){
                    callback();
                }else{
                    if (value === '') {
                        callback(new Error('请再次输入密码'));
                    } else if (value !== this.formData.pwd) {
                        callback(new Error('两次输入密码不一致!'));
                    } else {
                        callback();
                    }
                }

            };
            var validatePass = (rule, value, callback) => {
                if (this.formData.id != 0) {
                    callback();
                }else{
                   if(value === ''){
                       callback(new Error('请输入密码'));
                   }else{
                       callback();
                   }
                }
            }
            return{
                dialogFormVisible: false,
                formData:{
                    id:0,
                    account: '',
                    pwd: '',
                    conf_pwd: '',
                    roles: [],
                    status: '1',
                    real_name: ''
                },
                rules:{
                    account: [
                        { required: true, message: '管理员账号不可以为空', trigger: 'blur' },
                    ],
                    pwd: [
                        { validator: validatePass, trigger: 'blur' },
                    ],
                    conf_pwd: [
                        { validator: validateConfPass, trigger: 'blur' }
                    ],
                    roles: [
                        { required: true, message: '请选择至少一个管理员身份', trigger: 'change' },
                    ],
                    real_name: [
                        { required: true, message: '管理员姓名不能为空', trigger: 'blur' },
                    ],
                },
                loading:false,
                rowsCount: 0,
                rows: [],
                where:{
                    page:1,
                    limit: pageSize,
                    status: '',
                    name: '',
                    roles: '',
                },
                roles:roles,
            }
        },
        methods: {
            doLoadRows (pn) {
                this.where.page = pn || 1
                axios.get(Url({a:'get_list' , q:this.where})).then(res => {
                    this.rows = res.data.data
                    this.rowsCount = res.data.count
                })
            }
            // 设置状态
            ,doSetStatus (row) {
                axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}})).then(res => {
                    this.$message({
                        message: res.data.msg,
                        type: res.data.code === 200 ? 'success' : 'error',
                        onClose: _ => {
                            if (res.data.code !== 0) {
                                this.doLoadRows()
                            }
                        }
                    })
                })
            }
            // 编辑
            ,doAdd ( item ) {
                if ( item != undefined ) {
                    this.formData = item;
                } else {
                    this.formData = {
                        id:0,
                        account: '',
                        pwd: '',
                        conf_pwd: '',
                        roles: [],
                        real_name: '',
                        status: 1
                    }
                }
                this.dialogFormVisible = true
            }
            ,onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData)
                            .then(res => {
                                if(res.data.code == 200){
                                    this.$message({
                                        message:'操作成功',
                                        type:'success',
                                        duration:1500
                                    });
                                }else{
                                    this.$message({
                                        message:res.data.msg,
                                        type:'error',
                                        duration:1500
                                    });
                                }

                                this.dialogFormVisible = false;
                                this.doLoadRows(1)
                            }).catch(_ => {
                                this.$message({
                                    message: '操作失败',
                                    type: 'error'
                                })
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
            // 关闭
            ,closeDialog () {
                this.$refs['formData'].clearValidate();
            }
        },
        mounted(){
            this.doLoadRows()
        }
    })
</script>
{/block}